{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "0",
   "metadata": {},
   "source": [
    "[![Open in Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/opengeos/geoai/blob/main/docs/workshops/GeoAI_Workshop_2025.ipynb)\n",
    "\n",
    "# Object Detection from Remote Sensing Imagery with GeoAI\n",
    "\n",
    "- 📓 **Notebook**: <https://opengeoai.org/workshops/GeoAI_Workshop_2025>  \n",
    "- 💻 **GitHub**: <https://github.com/opengeos/geoai>\n",
    "\n",
    "---\n",
    "\n",
    "## 🧭 Introduction\n",
    "\n",
    "This notebook provides hands-on materials for using the GeoAI package for object detection in remote sensing imagery. \n",
    "\n",
    "## 🗂️ Agenda\n",
    "\n",
    "The workshop will guide you through the full pipeline of GeoAI for object detection in remote sensing imagery, including:\n",
    "\n",
    "- 📦 Package installation  \n",
    "- ⬇️ Data download  \n",
    "- 🖼️ Data visualization  \n",
    "- 🧠 Model training  \n",
    "- 🔍 Model inference  \n",
    "- 🛠️ Post-processing  \n",
    "- 🌐 Real-world applications  \n",
    "\n",
    "## ⚙️ Prerequisites\n",
    "\n",
    "- A Google Colab account (recommended for ease of setup)\n",
    "- Basic familiarity with Python and geospatial data\n",
    "\n",
    "## 📦 Package installation \n",
    "\n",
    "You can install the required packages using either `conda` or `pip`:\n",
    "\n",
    "### Option 1: Using Conda (recommended for local environments)\n",
    "\n",
    "```bash\n",
    "conda create -n geoai python=3.12\n",
    "conda activate geoai\n",
    "conda install -c conda-forge mamba\n",
    "mamba install -c conda-forge geoai\n",
    "```\n",
    "\n",
    "### Option 2: Using pip (for Colab or quick installation)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "1",
   "metadata": {},
   "outputs": [],
   "source": [
    "# %pip install geoai-py overturemaps"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2",
   "metadata": {},
   "source": [
    "## ⬇️ Data download\n",
    "\n",
    "### Import library"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "3",
   "metadata": {},
   "outputs": [],
   "source": [
    "import geoai"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4",
   "metadata": {},
   "source": [
    "### Retrieve collections\n",
    "\n",
    "Get all STAC collections from [Microsoft Planetary Computer](https://planetarycomputer.microsoft.com/catalog)."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "5",
   "metadata": {},
   "outputs": [],
   "source": [
    "collections = geoai.pc_collection_list()\n",
    "collections"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6",
   "metadata": {},
   "source": [
    "### Search NAIP imagery"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "7",
   "metadata": {},
   "outputs": [],
   "source": [
    "m = geoai.LeafMap(center=[47.653010, -117.592167], zoom=16)\n",
    "m"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "8",
   "metadata": {},
   "outputs": [],
   "source": [
    "bbox = m.user_roi_bounds()\n",
    "if bbox is None:\n",
    "    bbox = [-117.6021, 47.6502, -117.5824, 47.6559]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "9",
   "metadata": {},
   "outputs": [],
   "source": [
    "items = geoai.pc_stac_search(\n",
    "    collection=\"naip\",\n",
    "    bbox=bbox,\n",
    "    time_range=\"2013-01-01/2024-12-31\",\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "10",
   "metadata": {},
   "outputs": [],
   "source": [
    "items"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "11",
   "metadata": {},
   "outputs": [],
   "source": [
    "items[0]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "12",
   "metadata": {},
   "source": [
    "### Visualize NAIP imagery"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "13",
   "metadata": {},
   "outputs": [],
   "source": [
    "geoai.pc_item_asset_list(items[0])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "14",
   "metadata": {},
   "outputs": [],
   "source": [
    "geoai.view_pc_item(item=items[0])"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "15",
   "metadata": {},
   "source": [
    "### Download NAIP imagery"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "16",
   "metadata": {},
   "outputs": [],
   "source": [
    "downloaded = geoai.pc_stac_download(\n",
    "    items[0], output_dir=\"data\", assets=[\"image\", \"thumbnail\"]\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "17",
   "metadata": {},
   "outputs": [],
   "source": [
    "items[0]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "18",
   "metadata": {},
   "source": [
    "### Search Landsat data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "19",
   "metadata": {},
   "outputs": [],
   "source": [
    "items = geoai.pc_stac_search(\n",
    "    collection=\"landsat-c2-l2\",\n",
    "    bbox=bbox,\n",
    "    time_range=\"2023-07-01/2023-07-15\",\n",
    "    query={\"eo:cloud_cover\": {\"lt\": 1}},\n",
    "    max_items=10,\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "20",
   "metadata": {},
   "outputs": [],
   "source": [
    "items"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "21",
   "metadata": {},
   "outputs": [],
   "source": [
    "items[0]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "22",
   "metadata": {},
   "source": [
    "### Visualize Landsat data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "23",
   "metadata": {},
   "outputs": [],
   "source": [
    "geoai.pc_item_asset_list(items[0])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "24",
   "metadata": {},
   "outputs": [],
   "source": [
    "geoai.view_pc_item(item=items[0], assets=[\"red\", \"green\", \"blue\"])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "25",
   "metadata": {},
   "outputs": [],
   "source": [
    "geoai.view_pc_item(item=items[0], assets=[\"nir08\", \"red\", \"green\"])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "26",
   "metadata": {},
   "outputs": [],
   "source": [
    "geoai.view_pc_item(\n",
    "    item=items[0],\n",
    "    expression=\"(nir08-red)/(nir08+red)\",\n",
    "    rescale=\"-1,1\",\n",
    "    colormap_name=\"greens\",\n",
    "    name=\"NDVI Green\",\n",
    ")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "27",
   "metadata": {},
   "source": [
    "### Download Landsat data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "28",
   "metadata": {},
   "outputs": [],
   "source": [
    "geoai.pc_stac_download(\n",
    "    items[0], output_dir=\"data\", assets=[\"nir08\", \"red\", \"green\", \"blue\"], max_workers=1\n",
    ")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "29",
   "metadata": {},
   "source": [
    "### Download building data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "30",
   "metadata": {},
   "outputs": [],
   "source": [
    "buildings_gdf = geoai.get_overture_data(\n",
    "    overture_type=\"building\",\n",
    "    bbox=bbox,\n",
    "    output=\"data/buildings.geojson\",\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "31",
   "metadata": {},
   "outputs": [],
   "source": [
    "buildings_gdf.head()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "32",
   "metadata": {},
   "source": [
    "### Extract building statistics"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "33",
   "metadata": {},
   "outputs": [],
   "source": [
    "stats = geoai.extract_building_stats(buildings_gdf)\n",
    "print(stats)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "34",
   "metadata": {},
   "source": [
    "## 🖼️ Data Visualization\n",
    "\n",
    "### Download sample datasets from [Hugging Face](https://huggingface.co/datasets/giswqs/geospatial/tree/main)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "35",
   "metadata": {},
   "outputs": [],
   "source": [
    "train_raster_url = (\n",
    "    \"https://huggingface.co/datasets/giswqs/geospatial/resolve/main/naip_train.tif\"\n",
    ")\n",
    "train_vector_url = \"https://huggingface.co/datasets/giswqs/geospatial/resolve/main/naip_train_buildings.geojson\"\n",
    "test_raster_url = (\n",
    "    \"https://huggingface.co/datasets/giswqs/geospatial/resolve/main/naip_test.tif\"\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "36",
   "metadata": {},
   "outputs": [],
   "source": [
    "train_raster_path = geoai.download_file(train_raster_url)\n",
    "train_vector_path = geoai.download_file(train_vector_url)\n",
    "test_raster_path = geoai.download_file(test_raster_url)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "37",
   "metadata": {},
   "source": [
    "### View metadata"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "38",
   "metadata": {},
   "outputs": [],
   "source": [
    "geoai.print_raster_info(train_raster_path, figsize=(18, 10))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "39",
   "metadata": {},
   "outputs": [],
   "source": [
    "geoai.print_vector_info(train_vector_path, figsize=(18, 10))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "40",
   "metadata": {},
   "source": [
    "### Interactive visualization"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "41",
   "metadata": {},
   "outputs": [],
   "source": [
    "geoai.view_vector_interactive(train_vector_path, tiles=train_raster_url)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "42",
   "metadata": {},
   "outputs": [],
   "source": [
    "geoai.view_vector_interactive(\n",
    "    train_vector_path,\n",
    "    style_kwds={\"color\": \"red\", \"fillOpacity\": 0},\n",
    "    tiles=train_raster_url,\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "43",
   "metadata": {},
   "outputs": [],
   "source": [
    "geoai.view_vector_interactive(train_vector_path, tiles=\"Satellite\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "44",
   "metadata": {},
   "outputs": [],
   "source": [
    "geoai.view_raster(test_raster_url)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "45",
   "metadata": {},
   "source": [
    "## 🧠 Model training\n",
    "\n",
    "### Create training data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "46",
   "metadata": {},
   "outputs": [],
   "source": [
    "out_folder = \"output\"\n",
    "tiles = geoai.export_geotiff_tiles(\n",
    "    in_raster=train_raster_path,\n",
    "    out_folder=out_folder,\n",
    "    in_class_data=train_vector_path,\n",
    "    tile_size=512,\n",
    "    stride=256,\n",
    "    buffer_radius=0,\n",
    ")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "47",
   "metadata": {},
   "source": [
    "### Train object detection model"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "48",
   "metadata": {},
   "outputs": [],
   "source": [
    "geoai.train_MaskRCNN_model(\n",
    "    images_dir=f\"{out_folder}/images\",\n",
    "    labels_dir=f\"{out_folder}/labels\",\n",
    "    output_dir=f\"{out_folder}/models\",\n",
    "    num_channels=4,\n",
    "    pretrained=True,\n",
    "    batch_size=4,\n",
    "    num_epochs=10,\n",
    "    learning_rate=0.005,\n",
    "    val_split=0.2,\n",
    ")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "49",
   "metadata": {},
   "source": [
    "## 🔍 Model inference"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "50",
   "metadata": {},
   "outputs": [],
   "source": [
    "masks_path = \"naip_test_prediction.tif\"\n",
    "model_path = f\"{out_folder}/models/best_model.pth\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "51",
   "metadata": {},
   "outputs": [],
   "source": [
    "geoai.object_detection(\n",
    "    test_raster_path,\n",
    "    masks_path,\n",
    "    model_path,\n",
    "    window_size=512,\n",
    "    overlap=256,\n",
    "    confidence_threshold=0.5,\n",
    "    batch_size=4,\n",
    "    num_channels=4,\n",
    ")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "52",
   "metadata": {},
   "source": [
    "## 🛠️ Post-processing \n",
    "\n",
    "### Raster to vector conversion"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "53",
   "metadata": {},
   "outputs": [],
   "source": [
    "output_path = \"naip_test_prediction.geojson\"\n",
    "buildings_gdf = geoai.raster_to_vector(masks_path, output_path)\n",
    "buildings_gdf"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "54",
   "metadata": {},
   "outputs": [],
   "source": [
    "geoai.view_vector_interactive(buildings_gdf, tiles=test_raster_url)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "55",
   "metadata": {},
   "source": [
    "### Building regularization"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "56",
   "metadata": {},
   "outputs": [],
   "source": [
    "regularized_gdf = geoai.regularize(\n",
    "    data=buildings_gdf,\n",
    "    simplify_tolerance=2.0,\n",
    "    allow_45_degree=True,\n",
    "    diagonal_threshold_reduction=30,\n",
    "    allow_circles=True,\n",
    "    circle_threshold=0.9,\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "57",
   "metadata": {},
   "outputs": [],
   "source": [
    "geoai.view_vector_interactive(regularized_gdf, tiles=test_raster_url)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "58",
   "metadata": {},
   "outputs": [],
   "source": [
    "geoai.create_split_map(\n",
    "    left_layer=regularized_gdf,\n",
    "    right_layer=test_raster_url,\n",
    "    left_label=\"Regularized Buildings\",\n",
    "    right_label=\"NAIP Imagery\",\n",
    "    left_args={\"style\": {\"color\": \"red\", \"fillOpacity\": 0.3}},\n",
    "    basemap=test_raster_url,\n",
    ")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "59",
   "metadata": {},
   "source": [
    "### Result comparison"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "60",
   "metadata": {},
   "outputs": [],
   "source": [
    "m = geoai.LeafMap()\n",
    "m.add_cog_layer(test_raster_url, name=\"NAIP\")\n",
    "m.add_gdf(\n",
    "    buildings_gdf,\n",
    "    style={\"color\": \"yellow\", \"fillOpacity\": 0},\n",
    "    layer_name=\"Original\",\n",
    "    info_mode=None,\n",
    ")\n",
    "m.add_gdf(\n",
    "    regularized_gdf,\n",
    "    style={\"color\": \"red\", \"fillOpacity\": 0},\n",
    "    layer_name=\"Regularized\",\n",
    "    info_mode=None,\n",
    ")\n",
    "legend = {\n",
    "    \"Original\": \"#ffff00\",\n",
    "    \"Regularized\": \"#ff0000\",\n",
    "}\n",
    "m.add_legend(title=\"Building Footprints\", legend_dict=legend)\n",
    "m"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "61",
   "metadata": {},
   "source": [
    "### Calculate geometric properties"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "62",
   "metadata": {},
   "outputs": [],
   "source": [
    "props_gdf = geoai.add_geometric_properties(\n",
    "    regularized_gdf, area_unit=\"m2\", length_unit=\"m\"\n",
    ")\n",
    "props_gdf.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "63",
   "metadata": {},
   "outputs": [],
   "source": [
    "geoai.view_vector_interactive(props_gdf, column=\"area_m2\", tiles=test_raster_url)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "64",
   "metadata": {},
   "source": [
    "### Save results"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "65",
   "metadata": {},
   "outputs": [],
   "source": [
    "props_gdf.to_file(\"naip_test_buildings.geojson\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "66",
   "metadata": {},
   "source": [
    "## 🌐 Real-world applications  \n",
    "\n",
    "The section demonstrates how to apply [pre-trained models](https://huggingface.co/giswqs/geoai/tree/main) to real-world scenarios.\n",
    "\n",
    "\n",
    "### Building footprint extraction"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "67",
   "metadata": {},
   "outputs": [],
   "source": [
    "test_raster_url = (\n",
    "    \"https://huggingface.co/datasets/giswqs/geospatial/resolve/main/naip_test.tif\"\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "68",
   "metadata": {},
   "outputs": [],
   "source": [
    "test_raster_path = geoai.download_file(test_raster_url)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "69",
   "metadata": {},
   "outputs": [],
   "source": [
    "geoai.view_raster(test_raster_url)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "70",
   "metadata": {},
   "outputs": [],
   "source": [
    "masks_path = \"buildings_prediction.tif\"\n",
    "geoai.object_detection(\n",
    "    test_raster_path,\n",
    "    masks_path,\n",
    "    model_path=\"building_footprints_usa.pth\",\n",
    "    window_size=512,\n",
    "    overlap=256,\n",
    "    confidence_threshold=0.5,\n",
    "    batch_size=4,\n",
    "    num_channels=3,\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "71",
   "metadata": {},
   "outputs": [],
   "source": [
    "output_path = \"buildings_prediction.geojson\"\n",
    "buildings_gdf = geoai.raster_to_vector(masks_path, output_path)\n",
    "regularized_gdf = geoai.regularize(\n",
    "    data=buildings_gdf,\n",
    "    simplify_tolerance=2.0,\n",
    "    allow_45_degree=True,\n",
    "    diagonal_threshold_reduction=30,\n",
    "    allow_circles=True,\n",
    "    circle_threshold=0.9,\n",
    ")\n",
    "props_gdf = geoai.add_geometric_properties(\n",
    "    regularized_gdf, area_unit=\"m2\", length_unit=\"m\"\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "72",
   "metadata": {},
   "outputs": [],
   "source": [
    "geoai.view_vector_interactive(props_gdf, tiles=test_raster_url)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "73",
   "metadata": {},
   "source": [
    "### Solar panel detection"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "74",
   "metadata": {},
   "outputs": [],
   "source": [
    "test_raster_url = \"https://huggingface.co/datasets/giswqs/geospatial/resolve/main/solar_panels_davis_ca.tif\"\n",
    "test_raster_path = geoai.download_file(test_raster_url)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "75",
   "metadata": {},
   "outputs": [],
   "source": [
    "geoai.view_raster(test_raster_url)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "76",
   "metadata": {},
   "outputs": [],
   "source": [
    "masks_path = \"solar_panels_prediction.tif\"\n",
    "geoai.object_detection(\n",
    "    test_raster_path,\n",
    "    masks_path,\n",
    "    model_path=\"solar_panel_detection.pth\",\n",
    "    window_size=400,\n",
    "    overlap=100,\n",
    "    confidence_threshold=0.4,\n",
    "    batch_size=4,\n",
    "    num_channels=3,\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "77",
   "metadata": {},
   "outputs": [],
   "source": [
    "output_path = \"solar_panels_prediction.geojson\"\n",
    "regularized_gdf = geoai.orthogonalize(masks_path, output_path, epsilon=2)\n",
    "props_gdf = geoai.add_geometric_properties(\n",
    "    regularized_gdf, area_unit=\"m2\", length_unit=\"m\"\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "78",
   "metadata": {},
   "outputs": [],
   "source": [
    "geoai.view_vector_interactive(props_gdf, tiles=test_raster_url)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "79",
   "metadata": {},
   "source": [
    "### Car detection"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "80",
   "metadata": {},
   "outputs": [],
   "source": [
    "test_raster_url = (\n",
    "    \"https://huggingface.co/datasets/giswqs/geospatial/resolve/main/cars_test_7cm.tif\"\n",
    ")\n",
    "test_raster_path = geoai.download_file(test_raster_url)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "81",
   "metadata": {},
   "outputs": [],
   "source": [
    "geoai.view_raster(test_raster_url)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "82",
   "metadata": {},
   "outputs": [],
   "source": [
    "masks_path = \"cars_prediction.tif\"\n",
    "geoai.object_detection(\n",
    "    test_raster_path,\n",
    "    masks_path,\n",
    "    model_path=\"car_detection_usa.pth\",\n",
    "    window_size=512,\n",
    "    overlap=256,\n",
    "    confidence_threshold=0.5,\n",
    "    batch_size=4,\n",
    "    num_channels=3,\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "83",
   "metadata": {},
   "outputs": [],
   "source": [
    "output_path = \"cars_prediction.geojson\"\n",
    "gdf = geoai.orthogonalize(masks_path, output_path, epsilon=2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "84",
   "metadata": {},
   "outputs": [],
   "source": [
    "geoai.view_vector_interactive(output_path, tiles=test_raster_url)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "85",
   "metadata": {},
   "outputs": [],
   "source": [
    "geoai.create_split_map(\n",
    "    left_layer=output_path,\n",
    "    right_layer=test_raster_url,\n",
    "    left_args={\"style\": {\"color\": \"red\", \"fillOpacity\": 0.2}},\n",
    "    basemap=test_raster_url,\n",
    ")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "86",
   "metadata": {},
   "source": [
    "### Ship detection"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "87",
   "metadata": {},
   "outputs": [],
   "source": [
    "test_raster_url = \"https://huggingface.co/datasets/giswqs/geospatial/resolve/main/ships_sfo_test_15cm.tif\"\n",
    "test_raster_path = geoai.download_file(test_raster_url)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "88",
   "metadata": {},
   "outputs": [],
   "source": [
    "geoai.view_raster(test_raster_url)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "89",
   "metadata": {},
   "outputs": [],
   "source": [
    "masks_path = \"ship_prediction.tif\"\n",
    "geoai.object_detection(\n",
    "    test_raster_path,\n",
    "    masks_path,\n",
    "    model_path=\"ship_detection.pth\",\n",
    "    window_size=512,\n",
    "    overlap=256,\n",
    "    confidence_threshold=0.5,\n",
    "    batch_size=4,\n",
    "    num_channels=3,\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "90",
   "metadata": {},
   "outputs": [],
   "source": [
    "output_path = \"ship_prediction.geojson\"\n",
    "gdf = geoai.raster_to_vector(masks_path, output_path)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "91",
   "metadata": {},
   "outputs": [],
   "source": [
    "geoai.view_vector_interactive(output_path, tiles=test_raster_url)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "92",
   "metadata": {},
   "source": [
    "### Surface water mapping"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "93",
   "metadata": {},
   "outputs": [],
   "source": [
    "test_raster_url = \"https://huggingface.co/datasets/giswqs/geospatial/resolve/main/naip/naip_water_test.tif\"\n",
    "test_raster_path = geoai.download_file(test_raster_url)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "94",
   "metadata": {},
   "outputs": [],
   "source": [
    "geoai.view_raster(test_raster_url)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "95",
   "metadata": {},
   "outputs": [],
   "source": [
    "masks_path = \"water_prediction.tif\"\n",
    "geoai.object_detection(\n",
    "    test_raster_path,\n",
    "    masks_path,\n",
    "    model_path=\"water_detection.pth\",\n",
    "    window_size=512,\n",
    "    overlap=128,\n",
    "    confidence_threshold=0.3,\n",
    "    batch_size=4,\n",
    "    num_channels=4,\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "96",
   "metadata": {},
   "outputs": [],
   "source": [
    "output_path = \"water_prediction.geojson\"\n",
    "gdf = geoai.raster_to_vector(\n",
    "    masks_path, output_path, min_area=1000, simplify_tolerance=1\n",
    ")\n",
    "gdf = geoai.add_geometric_properties(gdf)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "97",
   "metadata": {},
   "outputs": [],
   "source": [
    "geoai.view_vector_interactive(gdf, tiles=test_raster_url)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "98",
   "metadata": {},
   "outputs": [],
   "source": [
    "geoai.create_split_map(\n",
    "    left_layer=gdf,\n",
    "    right_layer=test_raster_url,\n",
    "    left_args={\"style\": {\"color\": \"red\", \"fillOpacity\": 0.4}},\n",
    "    basemap=test_raster_url,\n",
    ")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "99",
   "metadata": {},
   "source": [
    "### Wetland mapping"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "100",
   "metadata": {},
   "outputs": [],
   "source": [
    "test_raster_url = \"https://huggingface.co/datasets/giswqs/geospatial/resolve/main/naip/m_4609932_nw_14_1_20100629.tif\"\n",
    "test_raster_path = geoai.download_file(test_raster_url)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "101",
   "metadata": {},
   "outputs": [],
   "source": [
    "geoai.view_raster(test_raster_url)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "102",
   "metadata": {},
   "outputs": [],
   "source": [
    "geoai.object_detection(\n",
    "    test_raster_path,\n",
    "    masks_path,\n",
    "    model_path=\"wetland_detection.pth\",\n",
    "    window_size=512,\n",
    "    overlap=256,\n",
    "    confidence_threshold=0.3,\n",
    "    batch_size=4,\n",
    "    num_channels=4,\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "103",
   "metadata": {},
   "outputs": [],
   "source": [
    "output_path = \"wetland_prediction.geojson\"\n",
    "gdf = geoai.raster_to_vector(\n",
    "    masks_path, output_path, min_area=1000, simplify_tolerance=1\n",
    ")\n",
    "gdf = geoai.add_geometric_properties(gdf)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "104",
   "metadata": {},
   "outputs": [],
   "source": [
    "geoai.view_vector_interactive(gdf, tiles=test_raster_url)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "105",
   "metadata": {},
   "outputs": [],
   "source": [
    "geoai.create_split_map(\n",
    "    left_layer=gdf,\n",
    "    right_layer=test_raster_url,\n",
    "    left_args={\"style\": {\"color\": \"red\", \"fillOpacity\": 0.4}},\n",
    "    basemap=test_raster_url,\n",
    ")"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
